<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
    <script>
      /**
       * 请补全JavaScript代码，要求将字符串参数URL中的参数解析并以对象的形式返回。
       * getParams('https://nowcoder.com/online?id=1&salas=1000')
       * {id:1, salas: 100}
       */
      const _getParams = (url) => {
        // 补全代码
        // 通过 URLSearchParams fromEntries 来获取
        // var queryStr = url.substring(url.indexOf('?'))
        // var params = new URLSearchParams(queryStr)
        // return Object.fromEntries(params)

        // 第二种通过 正则来查找匹配
        let splitUrl = url.slice(url.lastIndexOf('?') + 1).split('&')
        let obj = {}
        splitUrl.map((n) => {
          let [key, value] = n.split('=')
          obj[key] = value
        })
        return obj
      }
    </script>
  </body>
</html>
